package uiLayer;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import controlLayer.ProductCtr;

import javax.swing.JScrollPane;

import modelLayer.Product;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ProductDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private ProductCtr productCtr;
	private ProductTable productTable;
	private Product product;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		
		try {
			ProductDialog dialog = new ProductDialog(null, null);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} 
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public ProductDialog(ProductCtr productCtr, String productName) {
		
		setModalityType(ModalityType.APPLICATION_MODAL);
		setTitle("Tilføj vare");
	
		setProductCtr(productCtr);
		this.product = null;
		this.productTable = new ProductTable();
		setTableModel(productName);
		
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		this.contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(this.contentPanel, BorderLayout.CENTER);
		this.contentPanel.setLayout(new BorderLayout(0, 0));

		JScrollPane scrollPane = new JScrollPane();
		contentPanel.add(scrollPane);
		scrollPane.setViewportView(this.productTable);

		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						handleClickedOK();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						handleClickedCancel();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

	protected void handleClickedOK() {
		
		setProduct(this.productTable.getSelectedObject());
		this.setVisible(false);
		
	}
	

	protected void handleClickedCancel() {
		
		if (this.product != null) {
			this.product = null;
		}
		this.setVisible(false);
		
	}

	private void setTableModel(String productName) {
		
		List<Product> products = this.productCtr.findProductName(productName);
		this.productTable.setModel(products);
		
	}

	/**
	 * @param productCtr the productCtr to set
	 */
	private void setProductCtr(ProductCtr productCtr) {
		
		this.productCtr = productCtr;
		
	}
	
	/**
	 * @return the product
	 */
	public Product getProduct() {
		
		return this.product;
		
	}

	/**
	 * @param product the product to set
	 */
	private void setProduct(Product product) {
		
		this.product = product;
		
	}
	
}
